<%--
  Created by IntelliJ IDEA.
  User: nihaowzy
  Date: 2020/12/24
  Time: 10:06
  
--%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>客户列表</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resource/layui/css/layui.css">
</head>
<body>
<form class="layui-form layui-form-pane">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">
                客户名
            </label>
            <div class="layui-input-inline" style="width: 120px">
                <input type="text" class="layui-input" id="name" autocomplete="off">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">
                电话
            </label>
            <div class="layui-input-inline" style="width: 120px">
                <input type="text" class="layui-input" id="phone" autocomplete="off">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">
                性别
            </label>
            <div class="layui-input-inline" style="width: 120px">
                <select id="sex">
                    <option value="">性别</option>
                    <option value="1">男</option>
                    <option value="2">女</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">
                状态
            </label>
            <div class="layui-input-inline" style="width: 120px">
                <select id="deleted">
                    <option value="">状态</option>
                    <option value="1">有效</option>
                    <option value="2">无效</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">
                公司
            </label>
            <div class="layui-input-inline" style="width: 120px">
                <input type="text" class="layui-input" id="company" autocomplete="off">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">
                业务员
            </label>
            <div class="layui-input-inline" style="width: 120px">
                <select id="userId">
                    <option value="">业务员名称</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <button class="layui-btn" id="searchBtn" lay-submit type="button">查询</button>
        <button class="layui-btn layui-btn-primary" type="reset">重置</button>
    </div>
</form>
<table class="layui-table" id="dataTable" lay-filter="dataTableFilter"></table>
<%--表头工具栏--%>
<script type="text/html"  id="headBtns">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add" >
            <i class="layui-icon layui-icon-add-1"></i>
            新增客户
        </button>
        <c:if test="${user.role == 1}">
            <button class="layui-btn layui-btn-sm" lay-event="setSales" >
                <i class="layui-icon layui-icon-flag"></i>
                分配业务员
            </button>
        </c:if>
    </div>
</script>
<%--行工具栏--%>
<script type="text/html" id="rowBtns">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="edit">
            <i class="layui-icon layui-icon-refresh-1"></i>
            修改
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del">
            <i class="layui-icon layui-icon-delete"></i>
            删除
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-warm" lay-event="visit" >
            <i class="layui-icon layui-icon-flag"></i>
            拜访
        </button>
    </div>
</script>
<%--添加用户信息弹层界面--%>
<script type="text/html" id="addCustomer">
    <form  class="layui-form layui-form-pane" lay-filter="addCustomerFilter">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">客户姓名</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="name" type="text" placeholder="客户姓名" autocomplete="off" lay-verify="required" lay-reqText="客户姓名不能为空" />
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">电话</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="phone" type="text" placeholder="电话" autocomplete="off" lay-verify="required" lay-reqText="电话不能为空" />
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">公司</label>
                <div class="layui-input-inline"  >
                    <input class="layui-input" name="company" type="text"  placeholder="公司名称" autocomplete="off" lay-verify="required" lay-reqText="公司不能为空"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-inline" style="width: 210px">
                <input type="radio" name="sex" title="女"  value="2" checked/>
                <input type="radio" name="sex" title="男"  value="1" />
            </div>
        </div>
        <button style="display: none" type="button" id="subBtn" lay-submit lay-filter="subBtnFilter"></button>
    </form>
</script>
<%--分配业务员弹层模板--%>
<script type="text/html" id="setSalesmanTpl">
    <form class="layui-form layui-form-pane" lay-filter="editFormFilter">
        <div class="layui-form-item">
            <label class="layui-form-label">业务员</label>
            <div class="layui-input-inline" style="width: 210px">
               <select id="editUserId" name="userId" lay-verify="required" lay-reqText="业务员不能为空" >
                   <option value="">业务员</option>
               </select>
            </div>
        </div>
        <button style="display: none" type="button" id="subBtn" lay-submit lay-filter="subBtnFilter"></button>
    </form>
</script>
<%--新增拜访记录--%>
<script type="text/html" id="addVisitLogTpl">
    <form  class="layui-form layui-form-pane" lay-filter="addVisitLog">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">客户姓名</label>
                <div class="layui-input-inline">
                    <!--客户id-->
                    <input type="hidden" name="id">
                    <!--客户姓名-->
                    <input type="text" name="name" class="layui-input" readonly>

                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">拜访时间</label>
                <div class="layui-input-inline" >
                    <input type="text" readonly name="visitTime" id="visitTime" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item layui-form-text" style="width: 310px">
            <label class="layui-form-label">拜访内容</label>
            <div class="layui-input-block" >
                <textarea name="descp" class="layui-textarea"></textarea>
            </div>
        </div>
        <button style="display: none" type="button" id="subBtn" lay-submit lay-filter="subBtnFilter"></button>
    </form>
</script>

<script src="${pageContext.request.contextPath}/resource/layui/layui.js"></script>
<script>
    layui.use(['form','table','jquery','layer','laydate'],function () {
        let form = layui.form;
        let $ = layui.jquery;
        let table = layui.table;
        let layer = layui.layer;
        let laydate = layui.laydate;
        //项目根目录
        let rootPath = "${pageContext.request.contextPath}";
        //初始化业务员下拉列表
        function initSalesMan(){
            $.get(
                rootPath+"/user.do?service=salesMan",
                function (rs) {
                    console.log(rs);
                    if (rs.code == 200){
                        let data =  rs.data;
                        let select = $("#userId");
                        let options = new Array();
                        for (let user of data) {
                            options.push("<option value='"+user.id+"'>"+user.realname+"</option>")
                        }
                        let option = options.join();
                        select.append(option);
                        //渲染下拉框
                        form.render("select");
                    }
                }
            );
        }
        initSalesMan();
        //设置表格内容
        let opt = {
            id : "dataTableId",
            elem:"#dataTable",
            toolbar:"#headBtns",
            page:true,
            url:rootPath+"/customer.do?service=customerList",//后台数据接口
            cols:[[
                {type:"checkbox",fixed:"left"},
                {title:"客户名",field:"name"},
                {title:"电话号码",field:"phone"},
                {title:"性别",field:"sex",templet:function (d) {
                        let sex = d.sex;
                        if (sex == 1){
                            return "<b style='color: black'>男</b>"
                        }else if (sex == 2){
                            return "<b style='color: orange'>女</b>"
                        }
                    }},
                {title:"公司",field:"company"},
                {title: "状态",field:"deleted",templet:function (d) {
                        let deleted = d.deleted;
                        if (deleted == 1){
                            return "<b style='color: #00F7DE'>有效</b>"
                        }else if (deleted == 2){
                            return "<b style='color: orange'>无效</b>"
                        }
                    } },
                {title:"创建时间",field:"createTime"},
                {title:"修改时间",field:"updateTime"},
                {title:"删除时间",field:"deleteTime"},
                {title:"操作",toolbar:"#rowBtns",minWidth:300},

            ]],
            parseData:function (rs) {
                return {
                    code : rs.code,
                    msg : rs.msg,
                    count : rs.data.count,
                    data: rs.data.data
                }
            },
            response:{
                statusCode: 200
            }
        };
        let tabIns = table.render(opt);

        $("#searchBtn").click(function () {
            let name = $("#name").val();
            let phone = $("#phone").val();
            let sex = $("#sex").val();
            let deleted = $("#deleted").val();
            let company = $("#company").val();
            let userId = $("#userId").val();
            //根据查询条件表格进行重载
            tabIns.reload({
                where:{
                    name:name,
                    sex:sex,
                    phone:phone,
                    deleted:deleted,
                    company:company,
                    userId:userId,
                }
            });
        });



        //表头工具事件监听
        table.on('toolbar(dataTableFilter)',function (d){
            let event = d.event;
            if (event == "add") {
                add();
            }else if (event == "setSales"){
                setSales();
            }
        });
        //弹层模板
        let layerOpt = {
            title:"编辑",
            type:1,
            content:$("#addCustomer").html(),
            area:['360px','390px'],
            btn:['确认','取消'],
            btnAlign: "c",
            yes:function (index,layero) {
                //触发提交按钮点击
                $("#subBtn").click();
            }
        };

        //新增客户信息方法
        function add() {
            let addOpt = {};
            Object.assign(addOpt,layerOpt);
            //console.log(addOpt);
            addOpt.success = function(layero,index){
                //渲染表单
                form.render("radio");
                //表单的提交监听
                form.on("submit(subBtnFilter)",function (d) {
                    let formData = d.field;
                    //使用ajax提交数据
                    $.post(rootPath+"/customer.do?service=addCustomer",formData,function (rs) {
                        layer.msg(rs.msg);
                        if (rs.code != 200){
                            return false;
                        }
                        layer.close(index);//关闭弹层
                        $("#searchBtn").click();//刷新表格
                    })
                })
            }
            layer.open(addOpt);
        };
        //批量给客户分配业务员
        function setSales(){
            //获取选中的客户
            let status = table.checkStatus("dataTableId");
            //console.log(status);
            //获取选中行的数据
            let rowDatas = status.data;
            console.log(rowDatas)
            if (rowDatas.length == 0){
                layer.msg("请先选择需要分配业务员的客户");
                return false;
            }
            let opt = {
                title :"分配业务员 ！",
                type : 1,
                content: $("#setSalesmanTpl").html(),
                area:['340px','340px'],
                btn:['确认','取消'],
                btnAlign: 'c',
                success:function (layero,index) {
                    //首先渲染下拉框
                    let select = $("#userId");//搜索框中业务员下拉框列表
                    let select1 = $("#editUserId");//给客户分配业务员弹层的下拉框列表
                    let options = select.html();
                    select1 . html( options);//将业务员信息放入到下拉框中
                    form.render();
                    //表单的提交事件
                    form.on("submit(subBtnFilter)",function (d) {
                        let formData = d.field;//业务员id
                        console.log("表单数据")
                        console.log(formData);
                        let cid = new Array();
                        for (let c of rowDatas) {
                            cid.push("cId="+c.id)
                        }
                        //选中所有选中行的id; 例如cId=1&cId=3
                        let cId = cid.join("&");//将&作为分隔符
                        $.get(rootPath+"/customer.do?service=batchSalesMan&"+cId,formData,function (rs) {
                                layer.msg(rs.msg);
                                if (rs.code != 200){
                                    return false;
                                }
                                layer.close(index);
                                $("#searchBtn").click();
                            }
                        );
                    });
                },
                yes:function (index,layero) {
                    $("#subBtn").click();
                }
            }
            layer.open(opt);
        };

        //行工具监听事件
        table.on('tool(dataTableFilter)',function (d) {
            //console.log(d);
            let event = d.event;
            //console.log(event)
            let rowData = d.data;
            console.log(rowData);
            if (event == "edit"){
                edit(rowData);
            }else if (event == "del"){
                del(rowData);
            }else if (event == "visit"){
                visit(rowData);
            }
        });
        //新增拜访记录
        function visit(rowData) {
            console.log(rowData)
            let visitOpt = {}
            Object.assign(visitOpt,layerOpt);
            visitOpt.content = $("#addVisitLogTpl").html();
            visitOpt.success = function (layero,index) {
                //为表单赋值 根据name的属性来赋值的
               form.val("addVisitLog",rowData);
               //渲染时间
                laydate.render({elem:"#visitTime"});
                //为表单提交监听事件
                form.on('submit(subBtnFilter)',function (d) {
                    let formData = d.field;
                    //console.log(formData);
                    $.post(rootPath+"/customer.do?service=addVisitLog",formData,function (rs) {
                        layer.msg(rs.msg);
                        if (rs.code!=200){
                            return false;
                        }
                        layer.close(index);
                        $("#searchBtn").click();
                    })
                })
            }
            layer.open(visitOpt);
        };
        //客户更新
        function edit(rowData) {
            //console.log(rowData);
            let updateOpt = {}
            Object.assign(updateOpt,layerOpt)
            updateOpt.success = function(layero,index){
                form.render("radio");
                form.val("addCustomerFilter",rowData)
                form.on("submit(subBtnFilter)",function (d) {
                    let formData = d.field;
                    console.log(rowData)
                    console.log(formData)
                    // formData.id =  rowData.id
                    $.post(rootPath+"/customer.do?service=updateCustomer",formData,function (rs) {
                        layer.msg(rs.msg);
                        if (rs.code != 200){
                            return false;
                        }
                        layer.close(index);
                        $("#searchBtn").click();
                    });

                });
            }
            layer.open(updateOpt);
        }
        //客户删除
        function del(rowData) {
            let id = rowData.id;
            layer.confirm("确定要删除吗？",function (index) {
                $.get(rootPath+"/customer.do?service=deletedCustomer",{"id":id},function (rs) {
                    layer.msg(rs.msg);
                    if (rs.code != 200){
                        return false;
                    };
                    layer.close(index);
                    $("#searchBtn").click();
                });
            });

        }


    })
</script>
</body>
</html>
